package ejb.cbm.operatoria.session;

import java.util.List;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import ejb.cbm.operatoria.entity.ChequeEntity;
import ejb.cbm.operatoria.entity.ChequeEntityId;
import ejb.cbm.operatoria.local.ChequeSessionLocal;

////@Stateless
public class ChequeSession extends HibernateDaoSupport implements  ChequeSessionLocal {
	
	static Logger logger = Logger.getLogger(ChequeSession.class.getName());
	//property constants
	public static final String TIPOCHEQUE = "tipocheque";
	public static final String IMPORTE = "importe";
	public static final String OBSERVACIONCHEQUE = "observacioncheque";
	public static final String USUARIOREGISTRO = "usuarioregistro";
	public static final String USUARIOMODIFICACION = "usuariomodificacion";


    //@PersistenceContext 
	//private EntityManager entityManager;
	@Autowired
	public ChequeSession(SessionFactory sessionFactory){
		super.setSessionFactory(sessionFactory);
	}
	
    public void save(ChequeEntity transientInstance) {
    	logger.log("saving ChequeEntity instance", Level.INFO, null,null);
        try {
            getSession().persist(transientInstance);
            logger.log("save successful", Level.INFO, null,null);
        } catch (RuntimeException re) {
        	logger.log("save failed", Level.FATAL,null, re);
            throw re;
        }
    }
    
    public void delete(ChequeEntity persistentInstance) {
    	logger.log("deleting ChequeEntity instance", Level.INFO, null,null);
        try {
            getSession().delete(persistentInstance);
            logger.log("delete successful", Level.INFO, null,null);
        } catch (RuntimeException re) {
        	logger.log("delete failed", Level.FATAL,null, re);
            throw re;
        }
    }
    
    public ChequeEntity update(ChequeEntity detachedInstance) {
    	logger.log("updating ChequeEntity instance", Level.INFO, null,null);
        try {
            ChequeEntity result = (ChequeEntity) getSession().merge(detachedInstance);
            logger.log("update successful", Level.INFO, null,null);
            return result;
        } catch (RuntimeException re) {
        	logger.log("update failed", Level.FATAL,null, re);
            throw re;
        }
    }
    
    public ChequeEntity findById( ChequeEntityId id) {
    	logger.log("finding ChequeEntity instance with id: " + id, Level.INFO, null,null);
        try {
            ChequeEntity instance = (ChequeEntity) getSession().get(ChequeEntity.class, id);
            return instance;
        } catch (RuntimeException re) {
        	logger.log("find failed", Level.FATAL,null, re);
            throw re;
        }
    }
    
    //@SuppressWarnings("unchecked")
    public List<ChequeEntity> findByProperty(String propertyName, Object value) {
    	logger.log("finding ChequeEntity instance with property: " + propertyName
    						+ ", value: " + value, Level.INFO, null,null);
		try {
			String queryString = "select model from ChequeEntity model where model." 
			 						+ propertyName + "= :propertyValue";
			return getSession().createQuery(queryString)
 				.setParameter("propertyValue", value).list();
		} catch (RuntimeException re) {
			logger.log("find by property name failed", Level.FATAL,null, re);
			throw re;
		}
	}
	
	public List<ChequeEntity> findByTipocheque(Object tipocheque) {
		return findByProperty(TIPOCHEQUE, tipocheque);
	}
	
	public List<ChequeEntity> findByImporte(Object importe) {
		return findByProperty(IMPORTE, importe);
	}
	
	public List<ChequeEntity> findByObservacioncheque(Object observacioncheque) {
		return findByProperty(OBSERVACIONCHEQUE, observacioncheque);
	}
	
	public List<ChequeEntity> findByUsuarioregistro(Object usuarioregistro) {
		return findByProperty(USUARIOREGISTRO, usuarioregistro);
	}
	
	public List<ChequeEntity> findByUsuariomodificacion(Object usuariomodificacion) {
		return findByProperty(USUARIOMODIFICACION, usuariomodificacion);
	}
	

	//@SuppressWarnings("unchecked")
	public List<ChequeEntity> findAll() {
		logger.log("finding all ChequeEntity instances", Level.INFO, null,null);
		try {
			String queryString = "select model from ChequeEntity model";
			return getSession().createQuery(queryString).list();
		} catch (RuntimeException re) {
			logger.log("find all failed", Level.FATAL,null, re);
			throw re;
		}
	}
}